/**
 * Copyright (C) 2018-2019
 * All rights reserved, Designed By www.joolun.com
 */
package com.ruoyi.mall.controller;

import cn.dev33.satoken.id.SaIdUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import com.ruoyi.common.core.enums.DeviceType;
import com.ruoyi.common.satoken.utils.LoginHelper;
import com.ruoyi.mall.api.RemoteWxUserService;
import com.ruoyi.mall.api.dto.WxOpenDataDTO;
import com.ruoyi.mall.api.entity.WxUser;
import com.ruoyi.mall.entity.LoginMaDTO;
import com.ruoyi.mall.utils.AjaxResult;
import com.ruoyi.mall.utils.ThirdSessionHolder;
import com.ruoyi.mall.utils.WxMaUtil;
import com.ruoyi.system.api.model.LoginUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信用户
 *
 * @author www.joolun.com
 * @date 2019-08-25 15:39:39
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/api/ma/wxuser")
@Api(value = "wxuser", tags = "小程序用户API")
public class WxUserApi {
    @DubboReference
    private final RemoteWxUserService wxUserService;

    /**
     * 小程序用户登录
     *
     * @param request
     * @param loginMaDTO
     * @return
     */
    @ApiOperation(value = "小程序用户登录")
    @PostMapping("/login")
    public AjaxResult login(HttpServletRequest request, @RequestBody LoginMaDTO loginMaDTO) {
        try {
            WxUser wxUser = wxUserService.loginMa(WxMaUtil.getAppId(request), loginMaDTO.getJsCode());
            //TODO app 登录
            String id = wxUser.getId();
            LoginUser loginUser = new LoginUser();
            loginUser.setUserId(Long.parseLong(id));
            LoginHelper.loginByDevice(loginUser, DeviceType.APP);
            return AjaxResult.success(wxUser);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 获取用户信息
     *
     * @param
     * @return
     */
    @ApiOperation(value = "获取用户信息")
    @GetMapping
    public AjaxResult get() {
        //TODO 获取会话中 微信ID
//        String id = ThirdSessionHolder.getThirdSession().getWxUserId();
        String id = LoginHelper.getUserId().toString();
        return AjaxResult.success(wxUserService.getById(id));
    }

    /**
     * 保存用户信息
     *
     * @param wxOpenDataDTO
     * @return
     */
    @ApiOperation(value = "保存用户信息")
    @PostMapping
    public AjaxResult saveOrUptateWxUser(@RequestBody WxOpenDataDTO wxOpenDataDTO) {
        wxOpenDataDTO.setAppId(ThirdSessionHolder.getThirdSession().getAppId());
        wxOpenDataDTO.setUserId(ThirdSessionHolder.getThirdSession().getWxUserId());
        wxOpenDataDTO.setSessionKey(ThirdSessionHolder.getThirdSession().getSessionKey());
        WxUser wxUser = wxUserService.saveOrUptateWxUser(wxOpenDataDTO);
        return AjaxResult.success(wxUser);
    }
}
